package site.liuming.truismcms.utils;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BeanUtil {
    private static final String SET_PREFIX = "set";
    private static final String IS_PREFIX = "is";
    private static final String GET_PREFIX = "get";

    public BeanUtil() {
    }

    public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String propertyName) {
        PropertyDescriptor pd = null;

        try {
            Field field = clazz.getDeclaredField(propertyName);
            String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
            Method setMethod = clazz.getDeclaredMethod("set" + methodEnd, field.getType());
            Method getMethod = clazz.getDeclaredMethod("get" + methodEnd);
            pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return pd;
    }

    public static PropertyDescriptor getPropertyDescriptor2(Class<?> clazz, String propertyName) {
        try {
            return new PropertyDescriptor(propertyName, clazz);
        } catch (IntrospectionException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static void setProperty(Object obj, String propertyName, Object value) {
        Class<?> clazz = obj.getClass();
        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
        Method setMethod = pd.getWriteMethod();

        try {
            setMethod.invoke(obj, value);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

    }

    public static String getProperty(Object obj, String propertyName) {
        Class clazz = obj.getClass();

        try {
            PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);
            Method getMethod = pd.getReadMethod();
            String value = getMethod.invoke(clazz).toString();
            return value;
        } catch (Exception var6) {
            return "";
        }
    }

    public static String getValueByPropName(Object obj, String propName) {
        Class clazz = obj.getClass();

        try {
            String upperCaseFirstOne = toUpperCaseFirstOne(propName);
            Method method = clazz.getMethod("get" + upperCaseFirstOne);
            Object result = method.invoke(obj);
            String value = result.toString();
            return value;
        } catch (Exception var7) {
            return "";
        }
    }

    public static String toUpperCaseFirstOne(String s) {
        return Character.isUpperCase(s.charAt(0)) ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }
}
